package com.peanut.baby.mvp.reg;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.peanut.baby.AppConfig;
import com.peanut.baby.R;
import com.peanut.baby.mvp.main.MainActivity;
import com.peanut.devlibrary.BaseActivity;
import com.peanut.devlibrary.util.StringUtil;

import static com.peanut.baby.R.id.login_submit;


/**
 * Created by anonymouslystudio on 2017/7/5.
 */

public class RegistActivity extends BaseActivity
    implements RegistContract.View, View.OnClickListener{

    private RegistContract.Presenter    presenter;

    private EditText                    mobileEdit;
    private TextView                    codeButton;

    private String                      phone           = "";
    private boolean                     requestResult   = false;

    public static void start(Activity ctx, String oldPhone, boolean requestResult){
        Intent intent = new Intent(ctx,RegistActivity.class);
        intent.putExtra("requestResult",requestResult);
        intent.putExtra("oldPhone",oldPhone);
        if(requestResult){
            ctx.startActivityForResult(intent, AppConfig.RequestResult.REQUEST_CODE_REGIST);
        }else{
            ctx.startActivity(intent);
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        requestResult = getIntent().getBooleanExtra("requestResult",requestResult);
        presenter = new RegistPresenter(this);

        initializeView();
    }

    private void initializeView(){
        mobileEdit = (EditText) findViewById(R.id.phone_nubmber_edit);

        codeButton = (TextView) findViewById(R.id.get_vcode);
        codeButton.setOnClickListener(this);

        findViewById(R.id.login_submit).setOnClickListener(this);

    }


    @Override
    public void onRegistSuccess() {

    }

    @Override
    public void onRegistFailed(String failReason) {

    }

    @Override
    public void startCountDown() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                codeButton.setEnabled(false);
            }
        });
    }

    @Override
    public void onCountDown(final int current) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                codeButton.setText(current+"");
            }
        });
    }

    @Override
    public void onCountComplete() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                codeButton.setEnabled(true);
                codeButton.setText("重新获取验证码");
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.get_vcode:
                queryVcode();
                break;
            case login_submit:         //提交
                //TODO
                MainActivity.start(RegistActivity.this);
                finish();
                break;
        }
    }

    /**
     * 获取验证码
     */
    private void queryVcode(){
        String mobile = mobileEdit.getText().toString();
        if(StringUtil.isNullOrEmpty(mobile)){
            showToast("请输入手机号");
            return;
        }
       presenter.getVcode(mobile);
    }
}
